﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using PS.Engine.DataTypes;

namespace PS.Playground
{
    class Program
    {
        private const string SerializationDocumentPath = @"serialized.xml";

        static void Main(string[] args)
        {
            // Build Objects
            PS.Engine.DataTypes.Environment env = new PS.Engine.DataTypes.Environment();

            PedestrianRandomStartZone pedZone = new PedestrianRandomStartZone(50, 100, 100, 100);
            pedZone.Count = 10;
            pedZone.Target = new Vector2(300, 400);
            env.Zones.Add(pedZone);

            PedestrianSerializable ped = new PedestrianSerializable(0, 0, 770, 190);
            env.Pedestrians.Add(ped);

            // Dummy path: waypoints
            WaypointSerializable waypoint1 = new WaypointSerializable(0, 0, 400);
            WaypointSerializable waypoint2 = new WaypointSerializable(1, 50, 350);
            WaypointSerializable waypoint3 = new WaypointSerializable(2, 100, 400);
            WaypointSerializable waypoint4 = new WaypointSerializable(3, 150, 300);
            WaypointSerializable waypoint5 = new WaypointSerializable(4, 200, 380);
            WaypointSerializable waypoint6 = new WaypointSerializable(5, 250, 50);
            WaypointSerializable goal = new WaypointSerializable(6, 785, 233);
                        
            env.Waypoints.Add(waypoint1);
            env.Waypoints.Add(waypoint2);
            env.Waypoints.Add(waypoint3);
            env.Waypoints.Add(waypoint4);
            env.Waypoints.Add(waypoint5);
            env.Waypoints.Add(waypoint6);
            env.Waypoints.Add(goal);

            ObstacleSerializable obstacle1 = new ObstacleSerializable();
            obstacle1.Vertices.Add(new Vector2(70, 300));
            obstacle1.Vertices.Add(new Vector2(120, 380));
            obstacle1.Vertices.Add(new Vector2(180, 400));
            obstacle1.Vertices.Add(new Vector2(120, 310));
            //ObstacleSerializable obstacle1 = new ObstacleSerializable(70, 300);
            //ObstacleSerializable obstacle2 = new ObstacleSerializable(120, 380);
            //ObstacleSerializable obstacle3 = new ObstacleSerializable(180, 400);
            //ObstacleSerializable obstacle4 = new ObstacleSerializable(120, 310);
            //ObstacleSerializable obstacle5 = new ObstacleSerializable(250, 600);

            env.Obstacles.Add(obstacle1);
            //env.Obstacles.Add(obstacle2);
            //env.Obstacles.Add(obstacle3);
            //env.Obstacles.Add(obstacle4);
            //env.Obstacles.Add(obstacle5);

            // serialize objects
            string xml = GetSerializedXml(env);
            WriteToFile(xml, SerializationDocumentPath);
            Console.WriteLine("Xml serialisiert");

            string loadedXml = LoadXml(SerializationDocumentPath);
            PS.Engine.DataTypes.Environment envReLoaded = (PS.Engine.DataTypes.Environment)DeserializeXml(loadedXml, typeof(PS.Engine.DataTypes.Environment));
            Console.WriteLine("Xml deserialisiert");

            Console.ReadKey();
        }

        private static string GetSerializedXml(object objectToSerialize)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType());

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, objectToSerialize);
                stream.Position = 0;
                xmlDocument.Load(stream);

                return xmlDocument.InnerXml;
            }
        }

        private static string LoadXml(string path)
        {
            using (StreamReader reader = new StreamReader(path))            
            {
                return reader.ReadToEnd();
            }
        }

        private static void WriteToFile(string content, string path)
        {
            using (StreamWriter writer = new StreamWriter(path))
            {
                writer.Write(content);
            }
        }

        private static object DeserializeXml(string serializedXml, Type type)
        {
            using (StringReader stringReader = new StringReader(serializedXml))
            using (XmlReader xmlReader = new XmlTextReader(stringReader))
            {
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(xmlReader);
            }
        }
    }
}
